package com.hrpmar.service.impl;

import com.hrpmar.controller.dto.EmailDto;
import com.hrpmar.controller.vo.Message;
import com.hrpmar.mapper.MessageReceiverMapper;
import com.hrpmar.mapper.MessageReplyMapper;
import com.hrpmar.mapper.MessageSenderMapper;
import com.hrpmar.pojo.MessageReceiver;
import com.hrpmar.pojo.MessageReply;
import com.hrpmar.pojo.MessageSender;
import com.hrpmar.pojo.Worker;
import com.hrpmar.service.UtilsService;
import com.hrpmar.utils.Constants;
import com.hrpmar.utils.EmailUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * Created by 李松松 on 2017/12/4.
 */

@Service
@Transactional
public class UtilsServiceImpl implements UtilsService {

    @Resource
    private MessageReceiverMapper messageReceiverMapper;
    @Resource
    private MessageSenderMapper messageSenderMapper;
    @Resource
    private MessageReplyMapper messageReplyMapper;
    @Override
    public Integer SendEmail(EmailDto dto, HttpServletRequest req) {
        Integer i = 0;
        HttpSession session = req.getSession();
        Worker worker = (Worker) session.getAttribute(Constants.LOGIN_USER);
        dto.setBEmail(worker.getWoEmail());
        String se = dto.getSendingmode();
        String[] str = se.split("&");
        for (String s:str){
            if(1==Integer.parseInt(s)){
                String msg = EmailUtils.SendEmail(dto);
                if("发送失败!".equals(msg)){
                    i=-1;
                }
            }
            if(2==Integer.parseInt(s)) {
                System.out.println("短信啊送");
                try {
                    EmailUtils.SMSMessage(dto);
                } catch (IOException e) {
                    e.printStackTrace();
                    i = -1;
                }
            }
            if(3==Integer.parseInt(s)){
                try {
                    MessageSender messageSender = new MessageSender();
                    messageSender.setContent(dto.getText());//站内信息内容
                    messageSender.setTitle(dto.getTheme());//标题
                    messageSender.setSendDate(new Date());//发送时间
                    messageSender.setWoJubnum(worker.getWoJubnum());//发信人
                    messageSender.setFromDeleted(0);//是否删除
                    Integer mid = messageSenderMapper.addSenderInfo(messageSender);
                    MessageReceiver messageReceiver = new MessageReceiver();
                    messageReceiver.setMid(messageSender.getMid());//发件人编号
                    messageReceiver.setWoJubnum(dto.getWoJubnum());//收件人编号
                    messageReceiver.setIsReaded(0);//是否阅读
                    messageReceiver.setIsDeleted(0);//是 否  删除
                    System.out.println(messageReceiver.toString());
                    messageReceiverMapper.addReceiverInfo(messageReceiver);
                    MessageReply messageReply = new MessageReply();//回复信息表
                    messageReply.setMid(messageSender.getMid());//发信人信息表编号
                    messageReply.setReDate(new Date());
                    messageReply.setReContext(dto.getText());//标题
                    messageReply.setWoJubnum(worker.getWoJubnum());//写信人编号
                    messageReplyMapper.addmessageReply(messageReply);
                }catch (Exception e){
                    i=-1;
                }
            }
        }
        return i;
    }

}
